package com.lanchetech.merchant.controller;


import com.lanchetech.bean.request.ActivityReq;
import com.lanchetech.bean.response.BasePageResp;
import com.lanchetech.bean.response.BaseResp;
import com.lanchetech.bean.response.ResultData;
import com.lanchetech.bean.vo.ActivityVo;
import com.lanchetech.common.exception.ResultCodeException;
import com.lanchetech.merchant.service.ActivityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@Api(value = "活动接口", tags = "活动接口")
@RequestMapping("/api/activity")
@RestController
public class ActivityController {

    @Resource
    private ActivityService activityService;

    @ApiOperation("查看商家活动列表分页")
    @PostMapping("/getPage")
    public ResultData<BasePageResp<ActivityVo>> getPage(@RequestBody ActivityReq req){
        return activityService.getPage(req);
    }

    @ApiOperation("添加或编辑活动信息")
    @PreAuthorize("hasAnyAuthority( 'marketing.newlywed-zone.list.edit.auth')")
    @PostMapping("/editActivity")
    public BaseResp editActivity(@RequestBody ActivityVo activity) throws Exception {
        BaseResp baseResp = new BaseResp();
        try {
            activityService.editActivity(activity);
        } catch (ResultCodeException e) {
            baseResp.setResultCode(e.getResultCode());
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
        return baseResp;
    }


    @ApiOperation("删除活动信息")
    @GetMapping("/deleteActivity")
    @PreAuthorize("hasAnyAuthority( 'marketing.newlywed-zone.list.delete.auth')")
    public BaseResp deleteActivity(@RequestParam("id") Long id) {
        return activityService.deleteActivity(id);
    }


    @ApiOperation("查看活动信息详情")
    @GetMapping("/getDetail")
    @PreAuthorize("hasAnyAuthority( 'marketing.newlywed-zone.list.info.auth')")
    public ResultData<ActivityVo> getDetail(@RequestParam("id") Long id){
        return activityService.getDetail(id);
    }

    @ApiOperation("开启/关闭活动")
    @GetMapping("/onOrOffStatus")
    @PreAuthorize("hasAuthority('marketing.newlywed-zone.list.change.auth')")
    public BaseResp onOrOffStatus(@RequestParam("id")Long id){
        return activityService.onOrOffStatus(id);
    }
}
